<?php

set_include_path(get_include_path().PATH_SEPARATOR."_library/");

require_once '_library/Zend/Search/Lucene.php';

// Create index
$index = Zend_Search_Lucene::create('_index');

//require_once '_library/Zend/Search/Lucene/Document.php';

//$doc1 = new Zend_Search_Lucene_Document();
//
//// Field is not tokenized, but is indexed and stored within the index.
//// Stored fields can be retrived from the index.
//$doc1->addField(Zend_Search_Lucene_Field::Keyword('doctype',
//                                                 'autogenerated'));
//
//// Field is not tokenized nor indexed, but is stored in the index.
//$doc1->addField(Zend_Search_Lucene_Field::UnIndexed('created',
//                                                   time()));
//
//// Binary String valued Field that is not tokenized nor indexed,
//// but is stored in the index.
////$doc1->addField(Zend_Search_Lucene_Field::Binary('icon',
////                                                $iconData));
//
//// Field is tokenized and indexed, and is stored in the index.
//$doc1->addField(Zend_Search_Lucene_Field::Text('annotation',
//                                              'Document annotation text'));
//
//// Field is tokenized and indexed, but is not stored in the index.
//$doc1->addField(Zend_Search_Lucene_Field::UnStored('contents',
//                                                  'My document content'));
//
//// Add document to the index
//$index->addDocument($doc1);

$doc2 = Zend_Search_Lucene_Document_Html::loadHTMLFile("indexme.html", true);
$doc2->addField(Zend_Search_Lucene_Field::UnIndexed('created',
                                                   time()));

// Add document to the index
$index->addDocument($doc2);

$index->optimize();

// write the index to disk
$index->commit();

$hits  = array();

// Search the index
if(!empty($_GET["query"])) {
    $query = Zend_Search_Lucene_Search_QueryParser::parse(trim($_GET["query"]));
    $hits = $index->find($query);
}

Zend_Search_Lucene::setResultSetLimit(100);
echo $currentResultSetLimit = Zend_Search_Lucene::getResultSetLimit();

?>

<form method="get" action="">
<input type="text" name="query" value="<?=!empty($_GET["query"]) ? $_GET["query"] : ""?>" />
<br />
<input type="submit" />
</form>

<? foreach ($hits as $hit): ?>
    Score: <?=$hit->score?><br />
    Title: <?=$hit->title?><br /> 
    Created: <?=$hit->created?><br />
    <?=$query->highlightMatches(strip_tags($hit->body));?>
<? endforeach; ?>